package cn.serein.charging.order.dao.repository.impl;

import cn.serein.charging.order.constant.OrderStatusConstant;
import cn.serein.charging.order.dao.mappers.OrderFailMapper;
import cn.serein.charging.order.dao.mappers.OrderSuccessMapper;
import cn.serein.charging.order.dao.repository.OrderRepository;
import cn.serein.charging.order.pojo.po.ChargingBillFailPO;
import cn.serein.charging.order.pojo.po.ChargingBillSuccessPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


/**
 * 将业务和数据操作解耦，这样业务就不需要关心是数据是如何实现的
 */
@Repository("orderRepository")
public class OrderRepositoryImpl implements OrderRepository {

    @Autowired
    private OrderSuccessMapper orderSuccessMapper;

    @Autowired
    private OrderFailMapper orderFailMapper;


    @Override
    public void updateOrderStatusFromProcess2ExceptionEnd(String orderNo) {
        //  mapper.update(entity, wrapper)
        ChargingBillSuccessPO po = new ChargingBillSuccessPO();
        po.setBillStatus(OrderStatusConstant.ORDER_STATUS_EXCEPTION_END);

        // 相当于where子句  eq  等于   。。。。
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("bill_id", orderNo);
        wrapper.eq("bill_status", OrderStatusConstant.ORDER_STATUS_PROGRESS);

        orderSuccessMapper.update(po, wrapper);
    }

    @Override
    public ChargingBillSuccessPO getSuccessOrder(String orderNo) {
//        return null;
        // 使用mybatis-plus的查询方法
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("bill_id", orderNo);
        return orderSuccessMapper.selectOne(queryWrapper);
    }

    @Override
    public ChargingBillFailPO getFailOrder(String orderNo) {
//        return null;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("bill_id", orderNo);
        return orderFailMapper.selectOne(queryWrapper);
    }

    @Override
    public void saveFailOrder(ChargingBillFailPO newChargingBillFailPO) {
        orderFailMapper.insert(newChargingBillFailPO);

    }

    @Override
    public void updateDeviceInfo(String orderNo, Integer gunId) {

        // 更新目标值
        ChargingBillSuccessPO chargingBillSuccessPO = new ChargingBillSuccessPO();
        chargingBillSuccessPO.setGunId(gunId);
        UpdateWrapper updateWrapper = new UpdateWrapper();
        // 设置where子句
        updateWrapper.eq("bill_id", orderNo);
        orderSuccessMapper.update(chargingBillSuccessPO, updateWrapper);
    }
}
